/*
 * MIT License
 * 
 * Copyright (c) 2024-2025 milkpotatoes
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <ctime>
#include <cstdlib>

#include "include/js_fetch.h"

napi_value JsFetch::Fetch(napi_env env, napi_callback_info info)
{
    constexpr size_t minArgc = 1;
    size_t argc = minArgc;
    napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
    if (argc < minArgc)
    {
        napi_value error = nullptr;
        napi_throw_type_error(env, "clearTimeout",
                                "Failed to execute 'clearTimeout': 1 argument required, but only 0 present.");
        return nullptr;
    };
    napi_value *args = new napi_value[argc];
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    delete[] args;
    std::string sessionId = GenSessionId();
    napi_value jsSessionId = nullptr;
    napi_create_string_utf8(env, sessionId.c_str(), sessionId.size(), &jsSessionId);
    napi_async_work work = nullptr;
    napi_create_async_work(env, nullptr, jsSessionId, ExecuteFetch, AfterFetch, nullptr, &work);
    return jsSessionId;
};

void JsFetch::ExecuteFetch(napi_env env, void *data)
{
    return;
};
void JsFetch::AfterFetch(napi_env env, napi_status status, void *data)
{
    return;
};

std::string JsFetch::GenSessionId()
{
    constexpr char sessionIdHd[] = "SessionId_";
    constexpr size_t sessionIdHdLen = sizeof(sessionIdHd);

    constexpr size_t sessionIdLen = sessionIdHdLen + 8 + 1; // 8: session id length, SessionId_xxxxxx\0
    std::string sessionId = sessionIdHd;

    int id = rand();
    uint8_t *ids = reinterpret_cast<uint8_t *>(&id);
    for (size_t i = 0; i < sizeof(id); i++)
    {
        sessionId += ToHex(ids[i]);
    }
    return sessionId;
};

void JsFetch::BindToGlobal(napi_env env, napi_value exports)
{
    std::srand(time(0));

    napi_property_descriptor desc[] = {
        {"fetch", nullptr, Fetch, nullptr, nullptr, nullptr, napi_default_jsproperty, nullptr},
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return;
};